﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using smp.ButterflyCakes.Repository.Tests.Properties;
using smp.QueryDb.Tools;

namespace smp.ButterflyCakes.Repository.Tests
{
    [TestClass]
    public class RebuildDatabaseAndDb
    {
        [TestMethod]
        public void RebuildDb()
        {
            using(var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ButterflyCakes"].ConnectionString))
            {
                connection.Open();
                using(var destroyCommand = connection.CreateCommand())
                {
                    destroyCommand.CommandText = Resources.DestroyTables;
                    destroyCommand.CommandType = CommandType.Text;
                    destroyCommand.ExecuteNonQuery();
                }

                using (var createCommand = connection.CreateCommand())
                {
                    createCommand.CommandText = Resources.CreateDb;
                    createCommand.CommandType = CommandType.Text;
                    createCommand.ExecuteNonQuery();
                }

                using (var changeCommand = connection.CreateCommand())
                {
                    changeCommand.CommandText = Resources.ChangeDb;
                    changeCommand.CommandType = CommandType.Text;
                    //changeCommand.ExecuteNonQuery();
                }
            }

            var gen = new SqlStructureGenerator();
            var classString =
                gen.GetClassStringForAllAllObjects(
                    ConfigurationManager.ConnectionStrings["ButterflyCakes"].ConnectionString,
                    "smp.ButterflyCakes.Repository.Databases");
            classString = classString.Replace("public class ButterflyCakes", "public class Db");
            //using(var streamWriter = new StreamWriter(@"C:\Users\simonp\Dropbox\CODE\smp.ButterflyCakes\smp.ButterflyCakes.Repository\Databases\Dbs.cs", false))
            //{
            //    streamWriter.Write(classString);
            //}
            if (Environment.MachineName == "G7C875J")
            {
                System.IO.File.WriteAllText(
                    @"E:\DropBox\Dropbox\CODE\smp.ButterflyCakes\smp.ButterflyCakes.Repository\Databases\Db.cs",
                    classString);
            }
            else
            {
                System.IO.File.WriteAllText(
                    @"C:\Users\simonp\Dropbox\CODE\smp.ButterflyCakes\smp.ButterflyCakes.Repository\Databases\Db.cs",
                    classString);
            }          

        }
    }
}
